package cn.springboot.erp.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.SalesOrders;
import cn.springboot.erp.mapper.SalesOrdersMapper;
import cn.springboot.erp.service.SalesOrdersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("salesOrder")
@Api(tags = "订单管理")
public class SalesOrderController {

    @Resource
    private SalesOrdersService salesOrderService;

    @ApiOperation("数据分页")
    @PostMapping("page")
    public ResponseResult listPage(@RequestBody QueryParams<SalesOrders> queryParams){
        return salesOrderService.listPage(queryParams);
    }

    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    public ResponseResult deletById(@PathVariable("id") Integer id){
        SalesOrders orders = salesOrderService.getById(id);
        return ResponseResult.Success().data("order",orders);
    }

    @ApiOperation("根据id删除")
    @GetMapping("deleteById/{id}")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        return salesOrderService.deleteById(id);
    }

    @ApiOperation("添加或编辑")
    @PostMapping("insertOrUpdate")
    public ResponseResult insertOrUpdate(@RequestBody SalesOrders salesOrders){

        if (ObjectUtils.isNotNull(salesOrders.getId())){
            // 编辑
            salesOrderService.saveOrUpdate(salesOrders);
            return ResponseResult.Success().msg("编辑成功");
        }
        // 添加
        long time = new Date().getTime();
        salesOrders.setOrderNumber(String.valueOf(time));
        salesOrderService.saveOrUpdate(salesOrders);
        return ResponseResult.Success().msg("添加成功");
    }

    @ApiOperation("修改状态")
    @GetMapping("state/{id}/{state}")
    public ResponseResult state(@PathVariable("id") Integer id,@PathVariable("state") Integer state){
        SalesOrders salesOrders = new SalesOrders();
        salesOrders.setId(id);
        salesOrders.setPaymentStatus(state);
        salesOrderService.updateById(salesOrders);
        return ResponseResult.Success().msg("状态修改成功");
    }

    @ApiOperation("订单编号列表")
    @GetMapping("orderNameList")
    public ResponseResult orderNameList(){
        LambdaQueryWrapper<SalesOrders> lambda = new QueryWrapper<SalesOrders>().lambda();
        lambda.select(SalesOrders::getId,SalesOrders::getOrderNumber);
        List<SalesOrders> list = salesOrderService.list(lambda);
        return ResponseResult.Success().data("orderNameList",list);

    }
}
